<?php


namespace app\common\cachemodel;


use app\common\model\BaseCache;
use XznsTools\cache\CacheManage;

class ZlSearchAdvice extends BaseCache
{
    protected $table = 'wechat_backup_07_18_14_43_00.zl_search_advice';

    protected $cacheModel = 'content.search_advice';

    /**
     * 获取推荐搜索
     * @return array
     * @throws \Exception
     */
    public function getSearchAdvice(): array
    {
        $adviceList = CacheManage::getInstance()
            ->setCacheConfig($this->cacheModel)
            ->setPage(-1)
            ->setSort('desc')
            ->getListPage(function ($ids = [], $isCache = true, $sortType = 'desc')
            {
                return $this->getInfoByIdsInData($ids, [], $sortType);
            }, 'sort', []);

        return $adviceList['data'] ?? [];
    }


    /**
     * 重写数据时获取数据
     * @param $ids
     * @param $type
     * @param $sortType
     * @return array|false|string
     */
    public function getInfoByIdsInData($ids, $type, $sortType)
    {
        try
        {
            if ($ids)
            {
                $this->whereIn('id', $ids);
            } else
            {
                $this->where(['status' => 1]);
            }

            $data = $this->order('sort ' . $sortType)->column('content,target,link,status,is_yang,sort,create_time', 'id');
        }
        catch (\Exception $e)
        {
            $data = [];
        }

        return $data;
    }
}
